package com.dream.system.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.dream.common.Common;
import com.dream.common.SuperDao;
import com.dream.model.Weixin_Class;
import com.dream.model.Weixin_Teacher;
import com.dream.model.Weixin_User;
import com.dream.model.Weixin_VJR;
import com.dream.system.service.Weixin_ClassService;

@Component("weixin_ClassServiceImpl")
public class Weixin_ClassServiceImpl extends SuperDao<Object> implements Weixin_ClassService {

	@Override
	public void addschooleName(String schoolName) {
		// TODO Auto-generated method stub
		for (int i = 1; i <= 20; i++) {
			for (int j = 1; j <= 12; j++) {
				Weixin_Class wc = new Weixin_Class();
				wc.setBj(i + "");
				wc.setNj(j + "");
				wc.setSchoolName(schoolName);
				this.addT(wc);
			}
		}
	}

	@Override
	public Weixin_User addWeixin_User(Weixin_User model) {
		String tsql = "from Weixin_Teacher t where t.code = ?";
		Object[] objc = new Object[] { model.getCode() };
		Weixin_Teacher oj = (Weixin_Teacher) this.getListOne(tsql, objc, "");
		if (oj == null) {
			return null;
		}
		model.setNj(oj.getNj());
		model.setBj(oj.getBj());
		model.setSchoolName(oj.schoolName);
		model.setCode(oj.getCode());
		String sql = "from Weixin_User t where t.userTell = ?  ";
		Object[] obj = new Object[] { model.getUserTell() };
		List<Weixin_User> list = this.getList(sql, obj, 0, 10, "");
		if (null == list || list.size() == 0) {
			this.addT(model);
			return model;
		} else {
			for (int i = 0; i < list.size(); i++) {
				Weixin_User wu = list.get(i);
				wu.setState("0");
				this.updateT(wu);
			}
			this.addT(model);
			return model;
		}
	}

	@Override
	public JSONArray selectSchool(Weixin_User model) {
		// TODO Auto-generated method stub
		String hql = "select DISTINCT(t.schoolName) from Weixin_Class t where t.schoolName like '%" + model.getSchoolName() + "%'";
		Object[] obj = new Object[] { model.getSchoolName() };
		List list = this.getList(hql, 0, 10, "");
		if (null != list) {
			JSONArray jsonArray = JSONArray.fromObject(list);
			return jsonArray;
		}
		return null;

	}

	@Override
	public Weixin_Teacher addWeixin_Teacher(Weixin_Teacher model) {
		String sql = "from Weixin_Teacher t where t.userTell = ? and state='1' and t.nj = ?  and t.bj = ? and t.schoolName = ? ";
		Object[] obj = new Object[] { model.getUserTell(), model.getNj(), model.getBj(), model.getSchoolName() };
		List list = this.getList(sql, obj, 0, 10, "");
		if (null == list || list.size() == 0) {
			this.addT(model);
			return model;
		} else {
			return null;
		}
	}

	@Override
	public JSONObject getCodeBySchool(String zhucema) {
		String sql = "from Weixin_Teacher t where t.code = ? ";
		Object[] obj = new Object[] { zhucema };
		List list = this.getList(sql, obj, 0, 15, "");
		if (null == list && list.size() > 0) {
			return JSONObject.fromObject(list.get(0));
		} else {
			return null;
		}
		// TODO Auto-generated method stub

	}

	@Override
	public JSONArray getStudentByOpenId(Weixin_User model) {
		// TODO Auto-generated method stub
		if (null == model || model.getOpenId() == null || "".equals(model.getOpenId())) {
			return null;
		}
		String hql = "from Weixin_User t where t.openId = ? ";
		String hql1 = "from Weixin_Teacher t where t.openId = ? ";
		Object[] obj = new Object[] { model.getOpenId() };
		List object = this.getList(hql, obj, 0, 100, "");
		if (object != null && object.size() > 0) {
			return JSONArray.fromObject(object);
		}
		object = this.getList(hql1, obj, 0, 100, "");
		if (object != null && object.size() > 0) {
			return JSONArray.fromObject(object);
		}
		return null;

	}

	@Override
	public Object updateCode2(Weixin_User model) {

		try {
			if (null == model || model.getTable_id() == null || "".equals(model.getTable_id())) {
				return null;
			}
			Weixin_Teacher wu = (Weixin_Teacher) this.getT(new Weixin_Teacher(), model.getTable_id());
			if (wu != null) {
				wu.setCode2(model.getCode2());
				wu.setCodeSign(model.getCodeSign());
				wu.setString1(model.getString1());
				wu.setLong1(model.getLong1());
				wu.setCode2(model.getCode2());
				this.updateT(wu);
				return wu;
			}
			Weixin_User wt = (Weixin_User) this.getT(new Weixin_User(), model.getTable_id());
			if (wt != null) {
				wt.setCode2(model.getCode2());
				wt.setCodeSign(model.getCodeSign());
				wt.setString1(model.getString1());
				wt.setLong1(model.getLong1());
				this.updateT(wt);
				return wt;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public JSONObject getByCode2(String code2) {
		// TODO Auto-generated method stub
		if (null == code2 || code2 == null || "".equals(code2)) {
			return null;
		}
		String hql = "from Weixin_User t where t.code2 = ? ";
		String hql1 = "from Weixin_Teacher t where t.code2 = ? ";
		Object[] obj = new Object[] { code2 };
		Object object = this.getListOne(hql, obj, "");
		if (object != null) {
			return JSONObject.fromObject(object);
		}
		object = this.getListOne(hql1, obj, "");
		if (object != null) {
			return JSONObject.fromObject(object);
		}
		return null;

	}

	/**
	 * 
	 * 增加一个文件
	 * 
	 * @param model
	 * @return
	 */
	@Override
	public Object addVurl(Weixin_VJR model) {
		model.setCreateTime(Common.getTime1());
		model.setShowTime(Common.getTime());
		// 1视频，2图片，3语音，4文字
		if (true) {
			String s = "";
			if ("2".equals(model.getV_type())) {
				s = model.getV_url();
				System.out.println("--------------------" + s + "-------------------");
			} else {
				System.out.println("--------------------" + s + "-------------------");
				try {
					s = Common.sendGet1("http://www.12ddc.com/index.php/Sh/Weixin/getmedia/id/" + model.getV_url());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			// System.out.println(s);
			String id = UUID.randomUUID().toString();
			String geshi = ".x";
			String url = "/eduDown/" + id;
			if ("1".equals(model.getV_type())) {
				geshi = ".mp4";
			}
			if ("2".equals(model.getV_type())) {
				geshi = ".jpg";
			}
			if ("3".equals(model.getV_type())) {
				geshi = ".amr";
			}
			if ("4".equals(model.getV_type())) {
				geshi = "";
				model.setV_url1(model.getV_url());
			}
			url += geshi;

			try {
				boolean b = Common.downLoadFromUrl(s, id + geshi, "/eduDown/");
				if (b && "3".equals(model.getV_type())) {
					Common.changeToMp3(url, "/eduDown/" + id + ".mp3");
					model.setV_url1(id + ".mp3");
				} else {
					model.setV_url1(id + geshi);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("--------------下载微信平台的文件和转移格式错误------------------------");
			}
			// String str = QQImage.QQuploadVidoe("/zhuixun1/" + id +
			// ".mp3", url);

			// TODO Auto-generated method stub
			this.addT(model);
			return model;
		}
		return model;
	}

	/**
	 * 
	 * 查询个人的视频
	 */
	@Override
	public JSONArray getVurl(Weixin_VJR model) {
		// TODO Auto-generated method stub
		String hql = "from Weixin_VJR t where t.v_user = ? ";
		Object[] object = new Object[] { model.getV_user() };
		System.out.println(model.getStart());
		System.out.println(model.getLimit());
		List list = this.getList(hql, object, model.getStart(), model.getLimit(), "");
		if (list != null && list.size() > 0) {
			return JSONArray.fromObject(list);
		}
		return null;
	}

	@Override
	public JSONArray addStudentVJR(String v_url, String type, String banjtable_id) {
		// TODO Auto-generated method stub
		Weixin_Teacher wt = (Weixin_Teacher) this.getT(new Weixin_Teacher(), banjtable_id);
		if (wt == null) {
			return null;
		}
		String sql = "from Weixin_User t where t.schoolName = ?  and nj = ? and bj =? and state='1' ";
		Object[] obj = new Object[] { wt.getSchoolName(), wt.getNj(), wt.getBj() };
		List<Weixin_User> list = this.getList(sql, obj, 0, 1000, "");
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Weixin_User wu = list.get(i);
				Weixin_VJR wv = new Weixin_VJR();
				wv.setCreateTime(Common.getTime1());
				wv.setShowTime(Common.getTime());
				wv.setV_url1(v_url);
				wv.setV_type(type);
				wv.setV_user(wu.getUserTell());
				wv.setState("1");
				String messgageurl = "http://www.12ddc.com/index.php/Sh/Weixin/sendmsg";
				messgageurl += "/openid/" + wu.getOpenId();
				messgageurl += "/first/" + wu.getUserName() + "的家长,您好！";
				messgageurl += "/keyword1/" + wu.getNj() + "年级" + wu.getBj() + "班";
				messgageurl += "/keyword2/" + "本次作业已发送给您！";
				messgageurl += "/remark/" + "作业内容请点击“详情”查看。";
				Common.sendGet1(messgageurl);
				this.addT(wv);
			}
			return JSONArray.fromObject(list);
		}
		return null;
	}

	@Override
	public JSONArray getTeacherClass(String userCode) {
		// TODO Auto-generated method stub
		String hql = "from Weixin_Teacher t where t.userTell = ? and t.state ='1' ";
		Object[] object = new Object[] { userCode };
		List list = this.getList(hql, object, 0, 1000, "");
		if (list != null && list.size() > 0) {
			return JSONArray.fromObject(list);
		}

		return null;
	}

	@Override
	public int deleteVJR(Weixin_VJR model) {
		this.delete(model);
		return 1;
	}

}
